﻿var LG = {};
LG.loading = false;
LG.PrevImage = function (rootpath, paths)
{
    for (var i in paths)
    {
        $('<img />').attr('src', rootpath+paths[i]);
    }
}; 

LG.tip = function (message)
{
    if (wintip)
    {
        wintip.set('content', message);
        wintip.show();
    }
    else
    {
        wintip = $.ligerDialog.tip({ content: message });
    }
    setTimeout(function ()
    {
        wintip.hide()
    }, 4000);
}; 
//显示loading
LG.showLoading = function (message) {
    message = message || "正在加载中..."; 
    $('body').append("<div class='jloading'>" + message + "</div>");
    $.ligerui.win.mask();
};
//隐藏loading
LG.hideLoading = function (message) { 
    $('body > div.jloading').remove();
    $.ligerui.win.unmask({ id: new Date().getTime() });
}
LG.callServer = function (options) { 
    var p = options || {};
    p.type = p.type || 'AjaxDbHelper';
    //var url = p.type + "." + p.method + ".lg";
    var url = "AjaxHandler/AjaxHandler.ashx?" + $.param({ Type: p.type, Method: p.method });
    $.ajax({
        cache: false,
        url: url,
        data: p.data,
        dataType: 'json', type: 'post',
        beforeSend: function () {
            LG.loading = true;
            LG.showLoading(p.loading);
        },
        complete: function () {
            LG.loading = false;
            LG.hideLoading();
        },
        success: function (result) {
            if (!result.Error) {
                if (p.success)
                    p.success(result.Data, result.Message);
            }
            else {
                if (p.error)
                    p.error(result.Message);
            }
        },
        error: function (result) { 
            LG.tip('发现系统错误');
        }
    });
};


LG.PrevImage('lib/ligerUI/skins/Aqua/images/win/', ['dialog.gif', 'dialog-winbtns.gif', 'dialog-bc.gif', 'dialog-tc.gif']);
LG.PrevImage('lib/msgbox/', ['gb_tip_layer.png', 'gb_tip_layer_ie6.png', 'loading.gif']);





//使树支持 右击时选择
$.ligerui.controls.Tree.prototype._rendered = function ()
{
    $.ligerui.controls.Tree.base._rendered.call(this);
    var g = this, p = this.options;
    g.tree.bind('contextmenu.tree', function (e)
    {

        var obj = (e.target || e.srcElement);
        var treeitem = null;
        if (obj.tagName.toLowerCase() == "a" || obj.tagName.toLowerCase() == "span" || $(obj).hasClass("l-box"))
            treeitem = $(obj).parent().parent();
        else if ($(obj).hasClass("l-body"))
            treeitem = $(obj).parent();
        else
            treeitem = $(obj);
        if (!treeitem) return;
        var treedataindex = parseInt(treeitem.attr("treedataindex"));
        var treenodedata = g._getDataNodeByTreeDataIndex(g.data, treedataindex);
        var treeitembtn = $("div.l-body:first", treeitem).find("div.l-expandable-open:first,div.l-expandable-close:first");
        var clickOnTreeItemBtn = $(obj).hasClass("l-expandable-open") || $(obj).hasClass("l-expandable-close");
        if (!$(obj).hasClass("l-checkbox") && !clickOnTreeItemBtn)
        {
            if (!$(">div:first", treeitem).hasClass("l-selected")) 
            {
                if (g.trigger('beforeSelect', [{ data: treenodedata, target: treeitem[0]}]) == false)
                    return false;
                $(".l-body", g.tree).removeClass("l-selected");
                $(">div:first", treeitem).addClass("l-selected");
                g.trigger('select', [{ data: treenodedata, target: treeitem[0]}])
            }
        }
    });
}; 
